﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Model;

namespace DAL
{
   public class TagDAL
    {

        /// <summary>
        /// 获取所有标签
        /// </summary>
        /// <returns></returns>
        public List<Tag> GetList()
        {
            return DBUtility.MySqlHelper.ExecuteDataReader<Tag>("select * from Tag");
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Tag GetModel(string id)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<Tag>("select * from Tag where tag_id=@id", new MySqlParameter("@id",id));
        }

        /// <summary>
        /// 获取自定义数据
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="where">条件 </param>
        /// <returns></returns>
        public List<dynamic> GetList(string column, string where)
        {
            string sql = string.Format("select {0} from Tag where 1=1 {1}", column, where);
            return DBUtility.MySqlHelper.ExecuteDynamic(sql);
        }

        /// <summary>
        /// 标签名称获取标签id
        /// </summary>
        /// <param name="tagname"></param>
        /// <returns></returns>
        public int GetTagID(string tagname)
        {
            return DBUtility.MySqlHelper.ExecuteScalar<int>("select tag_id from Tag where tag_name=@tagname", new MySqlParameter("@tagname", tagname) );
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Tag model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@tag_name",model.tag_name),
                new MySqlParameter("@tag_count",model.tag_count),
                new MySqlParameter("@tag_mate",model.tag_mate)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery(" insert into Tag (tag_name,tag_count,tag_mate) Values(@tag_name,@tag_count,@tag_mate)", para);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Tag model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@tag_name",model.tag_name),
                new MySqlParameter("@tag_id",model.tag_id),
                new MySqlParameter("@tag_mate",model.tag_mate)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery("update Tag set tag_name=@tag_name,tag_mate=@tag_mate where tag_id=@tag_id", para);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delete(int id)
        {
            return DBUtility.MySqlHelper.ExecuteNonQuery("delete from Tag where tag_id=@id", new MySqlParameter("@id", id));
        }

        /// <summary>
        /// 更新标签文章数量
        /// </summary>
        /// <param name="tagid"></param>
        /// <returns></returns>
        public int UpdateTagCount(int tagid)
        {
            return DBUtility.MySqlHelper.ExecuteNonQuery("update Tag set tag_count=(select count(*) from blog where  blog_tags like '%【"+ tagid + "】%') where tag_id="+ tagid);
        }


    }
}
